/*
 * @Author: Kiro AI Assistant
 * @Date: 2025-01-10
 * @Description: MusicSearchViewModel 单元测试
 */

import 'package:flutter_test/flutter_test.dart';
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';

import '../../lib/viewmodels/music_search_view_model.dart';
import '../../lib/models/page_state.dart';

void main() {
  group('MusicSearchViewModel Tests', () {
    late MusicSearchViewModel viewModel;

    setUp(() {
      // 设置测试环境
      SharedPreferences.setMockInitialValues({});
      viewModel = MusicSearchViewModel();
    });

    tearDown(() {
      viewModel.dispose();
    });

    test('初始状态应该正确', () {
      expect(viewModel.searchList, isEmpty);
      expect(viewModel.searchCanPlayList, isEmpty);
      expect(viewModel.currentQuery, isEmpty);
      expect(viewModel.currentPage, equals(1));
      expect(viewModel.hasMore, isTrue);
      expect(viewModel.state, equals(PageState.idle));
    });

    test('清空搜索历史功能', () async {
      // 初始化
      viewModel.init();

      // 清空历史记录
      await viewModel.clearSearchHistory();

      // 验证历史记录已清空
      expect(viewModel.historyItems, isEmpty);
    });

    test('HTML高亮文本解析功能', () {
      const htmlStr = '这是<span class="c_tx_highlight">高亮</span>文本';
      const normalColor = Colors.black;
      const primaryColor = Colors.blue;

      final result = viewModel.configHtmlDataAnalysis(
        htmlStr: htmlStr,
        normalColor: normalColor,
        primaryColor: primaryColor,
      );

      expect(result.length, equals(3));
      expect(result[0].text, equals('这是'));
      expect(result[1].text, equals('高亮'));
      expect(result[2].text, equals('文本'));

      // 验证高亮部分的颜色
      expect((result[1].style as TextStyle).color, equals(primaryColor));
    });

    test('热门搜索管理功能', () {
      final hotItems = [
        const SearchItem(text: '热门歌曲1'),
        const SearchItem(text: '热门歌曲2'),
      ];

      viewModel.setHotItems(hotItems);

      expect(viewModel.hotItems.length, equals(2));
      expect(viewModel.hotItems.first.text, equals('热门歌曲1'));
    });

    test('清空搜索结果功能', () {
      // 清空搜索结果
      viewModel.clearSearchResults();

      expect(viewModel.searchList, isEmpty);
      expect(viewModel.currentQuery, isEmpty);
      expect(viewModel.state, equals(PageState.idle));
    });

    test('搜索状态文本功能', () {
      // 测试不同状态的文本
      expect(viewModel.searchStatusText, equals(''));

      viewModel.setState(PageState.loading);
      expect(viewModel.searchStatusText, equals('搜索中...'));

      viewModel.setState(PageState.empty);
      expect(viewModel.searchStatusText, equals('未找到相关音乐'));

      viewModel.setState(PageState.error, error: '网络错误');
      expect(viewModel.searchStatusText, equals('网络错误'));
    });

    test('SearchItem 相等性比较', () {
      const item1 = SearchItem(text: '测试');
      const item2 = SearchItem(text: '测试');
      const item3 = SearchItem(text: '其他');

      expect(item1, equals(item2));
      expect(item1, isNot(equals(item3)));
      expect(item1.hashCode, equals(item2.hashCode));
    });

    test('SearchItem JSON 序列化', () {
      const item = SearchItem(text: '测试', subtitle: '副标题');

      final json = item.toJson();
      expect(json['text'], equals('测试'));
      expect(json['subtitle'], equals('副标题'));

      final fromJson = SearchItem.fromJson(json);
      expect(fromJson.text, equals('测试'));
      expect(fromJson.subtitle, equals('副标题'));
    });

    test('获取搜索建议功能', () {
      // 由于搜索建议基于历史记录，初始状态下历史记录为空
      final suggestions = viewModel.getSearchSuggestions('测试');
      expect(suggestions, isEmpty);
    });
  });
}
